package com.cn.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cn.bean.Advice;
import com.cn.bean.Provider;
import com.cn.bean.Resident;
import com.cn.bean.Resident_collect;
import com.cn.dao.AccountDAO;
import com.cn.dao.ResidentDAO;
import com.cn.entity.ResidentEntity;
import com.cn.service.ResidentService;

public class ResidentServiceImpl implements ResidentService {
	private Map<String, Object> request, session;
	private ResidentDAO residentDao = null;
	private AccountDAO accountDao = null;
	HttpServletRequest requestS;

	public AccountDAO getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDAO accountDao) {
		this.accountDao = accountDao;
	}

	public void setResidentDao(ResidentDAO residentDao) {
		this.residentDao = residentDao;
	}

	/**
	 * 居民登录
	 */
	public List<ResidentEntity> login() throws Exception {
		requestS = ServletActionContext.getRequest();
		requestS.setCharacterEncoding("utf-8");
		String account = requestS.getParameter("account");			//从安卓端提交的数据获得帐号
		String password = requestS.getParameter("password");		//从安卓端提交的数据获得密码
		String hql = "from Resident as resident where account='" + account + "'and password = '" + password + "'";	//从数据库中查询相关帐号密码
		List list = residentDao.findByHql(hql);
		List<ResidentEntity> list1 = new ArrayList<ResidentEntity>();
		if (!list.isEmpty()) {										//如果查询结果不为空则表示登录成功
			Resident resident = (Resident) list.get(0);
			ResidentEntity re = new ResidentEntity();
			re.setId(resident.getId());
			re.setAccount(resident.getAccount());
			if (resident.getAddress() == null) {
				re.setAddress("");
			} else {
				re.setAddress(resident.getAddress());
			}
			if (resident.getRealname() == null) {
				re.setRealname("");
			} else {
				re.setRealname(resident.getRealname());
			}
			if (resident.getPhone() == null) {
				re.setPhone("");
			} else {
				re.setPhone(resident.getPhone());
			}
			list1.add(re);											//将居民实体放入list中并返回
		}
		return list1;
	}

	// 注册
	public String register() throws Exception {
		requestS = ServletActionContext.getRequest();
		requestS.setCharacterEncoding("utf-8");
		String account = requestS.getParameter("account");
		String password = requestS.getParameter("password");
		if (residentDao.findByAccount(account) != null) {
			return "existed";
		} 
		else if (accountDao.findByAccount(account) != null) {
			Resident resident = new Resident(account, password);
			if (residentDao.save(resident)) {
				return "succeed";
			} else {
				return "failed";
			}
		}
		else{
			return "notExisted";
		}
	}

	// 完善个人信息
	public String editInfor() throws Exception {
		requestS = ServletActionContext.getRequest();
		requestS.setCharacterEncoding("utf-8");
		String account = requestS.getParameter("account");
		String phone = requestS.getParameter("phoneNum");
		String address = requestS.getParameter("address");
		String realName = requestS.getParameter("realName");
		if (residentDao.findByPhone(phone) != null) {
			return "existed";
		} else {
			Resident resident = (Resident) residentDao.findByAccount(account);
			resident.setPhone(phone);
			resident.setAddress(address);
			resident.setRealname(realName);
			if (residentDao.update(resident)) {
				return "succeed";
			} else {
				return "failed";
			}

		}
	}

	/**
	 * 获取个人信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<ResidentEntity> getInfor() throws Exception {
		requestS = ServletActionContext.getRequest();
		requestS.setCharacterEncoding("utf-8");
		String account = requestS.getParameter("account");
		Resident resident = (Resident) residentDao.findByAccount(account);

		ResidentEntity re = new ResidentEntity();
		re.setId(resident.getId());
		re.setAccount(account);
		if (resident.getAddress() == null) {
			re.setAddress("");
		} else {
			re.setAddress(resident.getAddress());
		}
		if (resident.getRealname() == null) {
			re.setRealname("");
		} else {
			re.setRealname(resident.getRealname());
		}
		if (resident.getPhone() == null) {
			re.setPhone("");
		} else {
			re.setPhone(resident.getPhone());
		}
		List<ResidentEntity> list = new ArrayList<ResidentEntity>();
		list.add(re);
		return list;
	}

	//居民收藏服务商
	public String collect() throws Exception {
		requestS = ServletActionContext.getRequest();
		requestS.setCharacterEncoding("utf-8");
		int re_id = Integer.parseInt(requestS.getParameter("re_id"));
		int pro_id = Integer.parseInt(requestS.getParameter("pro_id"));
		Resident_collect rc=new Resident_collect();
		Resident re=new Resident();
		re.setId(re_id);
		rc.setResident(re);
		Provider pro=new Provider();
		pro.setId(pro_id);
		rc.setProvider(pro);
		if(residentDao.findCollect(re_id,pro_id)!=null)
		{
			if(residentDao.deleteCollection(re_id,pro_id))
				return "exist";
			else
				 return "failed";
		}
		else 
		{
			if(residentDao.save(rc))
			return "succeed";
			else
				return "failed";
		}
		
	}
	
	//居民提交意见反馈
	public String advice() throws Exception
	{
		requestS = ServletActionContext.getRequest();
		requestS.setCharacterEncoding("utf-8");
		int re_id = Integer.parseInt(requestS.getParameter("re_id"));
		String adviceContent=requestS.getParameter("adviceContent").toString();
		Advice advice=new Advice(new Resident(re_id),adviceContent);
		if(residentDao.save(advice))
			return "succeed";
		else
			return "failed";
	}
}
